import time
import sys
import json
import io

from DrissionPage._elements.chromium_element import ChromiumElement
from DrissionPage._pages.chromium_frame import ChromiumFrame
from DrissionPage._pages.chromium_tab import ChromiumTab

def click_titile_div(tab: ChromiumTab,click_title):
    title_ul_div = tab.ele("#struct-nav")
    title_lis = title_ul_div.eles("tag:li")
    for title_li in title_lis:
        if click_title == title_li.text:
            title_li.click()

def set_image_sort(tab: ChromiumTab):
    container_div = tab.ele('#container')
    filter_div = container_div.ele('tag:div@@class=filter')
    select_sort = filter_div.text
    sort_right_name = '按图片名升序'
    if sort_right_name != select_sort:
        filter_div.click()
        time.sleep(1)
        sort_list_ul = tab.ele('tag:ul@@class=next-menu-content')
        sort_list_lis = sort_list_ul.children()
        if sort_list_lis:
            for sort_li in sort_list_lis:
                if sort_right_name == sort_li.text:
                    sort_li.click()
                    time.sleep(1)

# 搜索图片的输入框: 填写名词
def search_image_input(tab: ChromiumTab, search_text, click_index: bool, select_one: bool):
    # 搜索图片区域(弹出大框)
    container_div = tab.ele('#container') # container

    # //*[@id="container"]/div/div[2]/div[2]/div/div[1]/div[2]/div[1]/div[3]
    key_filter = container_div.eles('tag:div@@class=key filter')
    # 搜索区域
    input_span = key_filter.ele('tag:span@@class=next-input next-input-single next-input-medium clear')
    # 文本输入框
    search_input = input_span.child("tag:input")
    search_input.click()  # 点击获取焦点
    image_name_search = search_text

    search_input.input(image_name_search)
    time.sleep(1)
    # 搜索图片的案例和框
    search_span = key_filter.ele('tag:span@@class=key-btn')
    search_btn = search_span.ele('tag:i')
    search_btn.click()

    time.sleep(4)  # 第一次记载图片

    items_div = tab.ele("#items")
    item_pic_actives = items_div.eles('tag:div@@class=item pic')

    if item_pic_actives:
        # 循环所有图片,根据名称判定是否正确
        for item_pic_div in item_pic_actives:
            image_name_div = item_pic_div.ele('tag:div@@class=name')
            if image_name_div.text.startswith(image_name_search):
                image_name_div.click()
                if select_one:  # 只选第一张
                    break
    if not select_one:
        submit_div = container_div.ele('tag:div@@text()=确认')
        if submit_div:
            submit_div.click();
            time.sleep(1)

def container_search_image_input(frame: ChromiumFrame, search_text, click_index: bool, select_one: bool):
    # 搜索图片区域(弹出大框)
    container_div = frame.ele('#container')
    # 文本输入框
    search_input = frame.ele('xpath:' + '//*[@id="container"]/div/div[2]/div[2]/div/div[1]/div[2]/div[1]/div[3]/span[1]/input')
    search_input.click()  # 点击获取焦点
    search_input.input(search_text)
    time.sleep(1)

    # 搜索图片的案例和框
    search_span = frame.ele('xpath:' +'//*[@id="container"]/div/div[2]/div[2]/div/div[1]/div[2]/div[1]/div[3]/span[2]/i')
    search_span.click()
    time.sleep(2)  # 第一次记载图片

    items_div = frame.ele("#items")
    item_pic_actives = items_div.eles('tag:div@@class=item pic')

    if item_pic_actives:
        # 循环所有图片,根据名称判定是否正确
        for item_pic_div in item_pic_actives:
            image_name_div = item_pic_div.ele('tag:div@@class=name')
            if image_name_div.text.startswith(search_text):
                image_name_div.click()
                if select_one:  # 只选第一张
                    break
    if not select_one:
        submit_div = container_div.ele('tag:div@@text()^确定') # 确认开始
        if submit_div:
            submit_div.click();
            time.sleep(1)